package com.talkyun.openx.plugin.echo;

import com.talkyun.utils.json.JSON;
import com.talkyun.utils.json.JSONObject;

import java.util.Date;

/**
 * Build-in Service: echo
 * <p/>
 * Created by kevin on 2015/1/1.
 */
public class EchoImpl implements Echo {

    @Override
    public String hello() {
        return "Hello  (" + new Date() + ")";
    }

    @Override
    public String helloByString(String user) {
        if ("exception".equalsIgnoreCase(user)) {
            throw new RuntimeException("RuntimeException Echo!");
        } else if ("AppException".equalsIgnoreCase(user)) {
            throw new RuntimeException("App Exception Echo!") {
                @Override
                public String toString() {
                    JSONObject json = JSON.parseObject("{}");
                    json.put("code", "1000" + "1000");
                    json.put("class", this.getClass().getName());
                    json.put("message", this.getMessage());
                    return json.toString();
                }
            };
        }
        return "Hello " + user + " (" + new Date() + ")";
    }

    @Override
    public int helloByInt(int num) {
        return num + 1000;
    }

    @Override
    public Integer helloByInteger(Integer num) {
        if (num != null) {
            return num + 1000;
        }
        return null;
    }

    @Override
    public User helloByPojo(long id, User user) {
        user.setId(id);
        user.setUserName("Hello " + user.getUserName());
        user.setPassWord("Hello " + user.getPassWord());
        return user;
    }
}
